<?php

namespace app\index\controller;

use think\Db;

class Article extends Base
{


    /**
     *获取咨讯分类
     */
    public function getArticleCate()
    {

        $getArticleCate = Db::name('article_cate')->paginate($this->pageSize, false, ['page' => $this->pageId]);
        $getArticleCate ? $this->suc($getArticleCate) : $this->err('获取咨讯分类失败');
    }

    /**
     *获取咨讯文章列表
     */
    public function getArticleList()
    {
        $where = [];
        $cid=0;
        input('cid') ? $cid = input('cid') : $this->err('缺少用户编码！');
        input('categoryId')!=null ? $where['a.categoryId'] = input('categoryId') : $this->err('缺少分类编码！');
        $field = 'a.id,a.content,a.title,a.categoryId,a.videoPicSrc,a.videoSrc,a.articleType,a.clickCount,a.commentCount,a.collectCount,a.visitCount,a.status,a.sort,a.createTime,a.publisher,ua2.isCollect';
        $res = model('article')
            ->alias('a')
            ->field($field)
            ->join('user_article ua', 'ua.articleId=a.id ' , 'left')
            ->join('user_article ua2', 'ua2.articleId=a.id and ' . $cid . ' = ua2.cid', 'left')
            ->join('article_cate ac','ac.id = a.categoryId','left')
            ->where($where)
            ->group('a.id')
            ->order('a.sort desc,a.createTime desc')
            ->paginate(10)
            ->toArray();

        $res ? $this->suc($res) : $this->err('获取文章列表失败！');
    }

    /**
     *获取文章详情
     */
    public function getArticleDetail()
    {
        $where = [];
        input('articleId') ? $where['articleId'] = input('articleId') : $this->err('缺少文章主键！');
        input('cid') ? $cid = input('cid') : $this->err('缺少用户主键！');
        //去文章表查询有没有文章
        $article = model('article')->where(['id' => $where['articleId']])->find();
        if (!$article) {
            $this->err('错误文章主键！');
        }
        //查询文章游览量
        $clickCount = model('article')->field('clickCount')->where(['id' => $where['articleId']])->find();
        $clickCount = $clickCount['clickCount'] + 1;
        $data = [
            'id' => input('articleId'),
            'clickCount' => $clickCount
        ];
        model('article')->editData($data, $data['id']);
        $res=model('article')
            ->alias('a')
            ->field('a.*,ua.isCollect')
            ->join('user_article ua','ua.articleId = a.id and '.$cid.' = ua.cid' ,'left')
            ->where(['a.id'=>$where['articleId']])
            ->find();
        $res ? $this->suc($res) : $this->err('获取文章详情出错！');
    }
    /**
     *评论
     */
    public function commentArticle()
    {
        $where = [];
        $ua_data = [];
        input('cid') ? $where['cid'] = input('cid') : $this->err('缺少用户主键');
        input('id') ? $where['articleId'] = input('id') : $this->err('缺少文章主键');
        input('commentValue') ? $ua_data['commentValue'] = input('commentValue') : $this->err('缺少评论内容');

        //查询用户是否存在
        $user = model('user')->where(['id' => $where['cid']])->find();
        if (!$user) {
            $this->err('错误用户主键！');
        }
        //查询文章是否存在
        $article = model('article')->where(['id' => $where['articleId']])->find();
        if (!$article) {
            $this->err('错误文章主键！');
        }
        //判断文章是否已经评论过
        $res = model('user_article')->field('id,isComment')->where($where)->find();
        if ($res['isComment'] == 1) {
            $this->err('您已评论过该文章！');
        }

        if (empty($res['id'])) {
            $data = [
                'cid' => input('cid'),
                'articleId' => input('id'),
                'isComment' => 1,
                'commentValue' => input('commentValue'),
                'commentTime'=>createTime()
            ];
            $ua_res = model('user_article')->editData($data);
        } else {
            $data = [
                'id' => $res['id'],
                'isComment' => 1,
                'commentValue' => input('commentValue'),
                'commentTime'=>createTime()
            ];
            $ua_res = model('user_article')->editData($data, $data['id']);
        }
        $ua_res ? true : $this->err('发表评论失败！');
        $article_data = [
            'commentCount' => ['inc', 1],
            'id' => input('id')
        ];
        $article_rep = model('article')->editData($article_data, $article_data['id']);
        $article_rep ? $this->suc('新增评论数成功') : $this->err('新增评论数失败！');
    }
    /**
     *点赞
     */
    public function collectArticle()
    {

        $where = [];
        input('cid') ? $where['cid'] = input('cid') : $this->err('缺少用户主键');
        input('articleId') ? $where['articleId'] = input('articleId') : $this->err('缺少文章主键');

        $user_article=model('user_article')->field('id,isCollect')->where($where)->find();
        $user_article['isCollect'] ? $this->err('该文章已经被点赞过') : true;

        //启动事务
        Db::startTrans();
        try{
            if($user_article){
                //如果用户和文章已存在关系执行编辑操作
                $res= model('user_article')->where(['cid'=>$where['cid'],'articleId'=>$where['articleId']])->Update(['isCollect'=>1]);
            }else{
                //如果用户和文章没有关系执行新增操作
                $data = [
                    'cid' => input('cid'),
                    'articleId' => input('articleId'),
                    'isCollect' => 1,
                ];
                $res = model('user_article')->editData($data);
            }
            $article_data = [
                'collectCount' => ['inc', 1],
                'id' => input('articleId')
            ];

            $article = model('article')->editData($article_data, $article_data['id']);
            $collectCount=model('article')->field('collectCount')->where(['id'=> $where['articleId']])->find();

            //提交事务
            if($res){
                Db::commit();
                $this->suc($collectCount);
            }else{
                Db::rollback();
                $this->err('点赞失败！');
            }


        }catch(\exception $e){
            Db::rollback();
            $this->err('操作失败！');
        }
    }

    /**
     *取消点赞
     */
    public function cancelCollect()
    {

        $where = [];
        input('cid') ? $where['cid'] = input('cid') : $this->err('缺少用户主键');
        input('articleId') ? $where['articleId'] = input('articleId') : $this->err('缺少文章主键');

        $user_article=model('user_article')->field('id,isCollect')->where($where)->find();

        if(empty($user_article['isCollect'])){
            $this->err('该文章尚未点赞！');
        }

        //启动事务
        Db::startTrans();
        try{
            $res= model('user_article')->where(['cid'=>$where['cid'],'articleId'=>$where['articleId']])->update(['isCollect'=>0]);
            $article_data = [
                'collectCount' => ['dec', 1],
                'id' => input('articleId')
            ];
            $article = model('article')->editData($article_data, $article_data['id']);
            $collectCount=model('article')->field('collectCount')->where(['id'=> $where['articleId']])->find();
            //提交事务
            if($res){
                Db::commit();
                $this->suc($collectCount);
            }else{
                Db::rollback();
                $this->err('取消失败！');
            }
        }catch(\exception $e){
            Db::rollback();
            $this->err('操作失败！');
        }
    }

    /**
     *获取文章评论列表
     */
    public function getCommentArticleList()
    {

        $where = [];
        input('id') ? $where['articleId'] = input('id') : $this->err('缺少文章主键');
        $article = model('article')->where(['id' => $where['articleId']])->find();
        if (!$article) {
            $this->err('用户编码错误！');
        }

        $where['isComment'] = 1;
        $where['commentShow'] = 1;
        $res = model('user_article')
            ->alias('ua')
            ->field('ua.*,u.name,u.avatar')
            ->join('user u', 'ua.cid=u.id', 'left')
            ->where($where)
            ->order('ua.id')
            ->paginate(10)
            ->toArray();

        $res ? $this->suc($res) : $this->err('获取评论列表失败');
    }

}
